From 47f3c99e1c58bb50e60d03727c2b2cdcef363cbf Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=98yvind=20Kol=C3=A5s?= Date: Sat, 17 Mar 2012 12:22:10 +0000 Subject: [PATCH] add API for managing palette based formats --- babl/Makefile.am | 1 + babl/babl-ids.h | 1 + babl/babl-types.h | 8 +++- babl/babl.h | 21 ++++++++ babl/base/babl-base.h | 1 + tests/Makefile.am | 3 +- tests/palette.c | 108 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 tests/palette.c diff --git a/babl/Makefile.am b/babl/Makefile.am index 93c2657..cefab57 100644 --- a/babl/Makefile.am +++ b/babl/Makefile.am @@ -27,6 +27,7 @@ c_sources = \ babl-memory.c \ babl-model.c \ babl-mutex.c \ + babl-palette.c \ babl-sampling.c \ babl-sanity.c \ babl-type.c \ diff --git a/babl/babl-ids.h b/babl/babl-ids.h index f0f6c61..f26add8 100644 --- a/babl/babl-ids.h +++ b/babl/babl-ids.h @@ -85,6 +85,7 @@ enum { BABL_CB, BABL_CR, BABL_PADDING, + BABL_PAL, BABL_COMPONENT_LAST_INTERNAL, BABL_FORMAT_BASE = 100000, diff --git a/babl/babl-types.h b/babl/babl-types.h index 7871e3b..ea30b7f 100644 --- a/babl/babl-types.h +++ b/babl/babl-types.h @@ -35,7 +35,9 @@ typedef union _Babl Babl; */ typedef long (*BablFuncLinear) (const char *src, char *dst, - long n); + long n, + void *src_model_data, + void *dst_model_data); /* TypePlanar,ModelPlanar and FormatPlanar */ typedef long (*BablFuncPlanar) (int src_bands, @@ -44,6 +46,8 @@ typedef long (*BablFuncPlanar) (int src_bands, int dst_bands, char *dst[], int dst_pitch[], - long n); + long n, + void *src_model_data, + void *dst_model_data); #endif diff --git a/babl/babl.h b/babl/babl.h index 49110d3..e4b0082 100644 --- a/babl/babl.h +++ b/babl/babl.h @@ -200,6 +200,27 @@ int babl_format_is_format_n (Babl *format); Babl * babl_conversion_new (void *first_arg, ...) BABL_ARG_NULL_TERMINATED; +/** + * create a new palette based format, name is optional pass in NULL to get + * an anonymous format. + */ +Babl *babl_new_palette (const char *name); + +/** + * Assign a palette to a palette format, the data is a single span of pixels + * representing the colors of the palette. + */ +void babl_palette_set_palette (Babl *babl, + Babl *format, + void *data, + int count); + +/** + * reset a palette to initial state. + */ +void babl_palette_reset (Babl *babl); + + /* * Backwards compatibility stuff diff --git a/babl/base/babl-base.h b/babl/base/babl-base.h index 57d4ceb..bed3c64 100644 --- a/babl/base/babl-base.h +++ b/babl/base/babl-base.h @@ -28,6 +28,7 @@ void babl_base_type_u8 (void); void babl_base_type_u16 (void); void babl_base_type_u32 (void); +void babl_base_model_pal (void); void babl_base_model_rgb (void); void babl_base_model_gray (void); void babl_base_model_ycbcr (void); diff --git a/tests/Makefile.am b/tests/Makefile.am index 8b9060d..ba88a9c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -10,6 +10,7 @@ TESTS = \ sanity \ babl_class_name \ types \ + palette \ extract \ nop \ n_components \ @@ -33,6 +34,4 @@ noinst_PROGRAMS = \ babl-html-dump \ conversions \ formats \ - extract \ - nop \ $(TESTS) diff --git a/tests/palette.c b/tests/palette.c new file mode 100644 index 0000000..02f79ac --- /dev/null +++ b/tests/palette.c @@ -0,0 +1,108 @@ +/* babl - dynamically extendable universal pixel conversion library. + * Copyright (C) 2005, Øyvind Kolås. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, see + * . + */ + +#include "config.h" +#include +#include +#include +#include "babl.h" + + +#define CHECK_CONV(test_name, componenttype, src_fmt, dst_fmt, src_pix, expected_pix) \ + { \ + Babl *fish; \ + int i; \ + fish = babl_fish (src_fmt, dst_fmt); \ + if (!fish) \ + { \ + printf (" %s failed to make fish\n", test_name); \ + OK = 0; \ + } \ + for (i = 0; i < sizeof(src_pix)/sizeof(src_pix[0]); i ++) \ + { \ + int c;\ + componenttype result[10]; \ + babl_process (fish, src_pix[i], result, 1); \ + for (c = 0; c < sizeof(expected_pix[i])/sizeof(expected_pix[i][0]); c++) \ + if (result[c] != expected_pix[i][c]) \ + { \ + printf (" %s failed #%i[%i] got %i expected %i\n", test_name, i, c, result[c], expected_pix[i][c]); \ + OK = 0; \ + } \ + } \ + } + + +int +main (int argc, + char **argv) +{ + int OK = 1; + babl_init (); + + { + unsigned char in[][1] = {{ 0},{ 1},{ 2},{15}}; + unsigned char out[][4] = {{0,0,0,255},{127,0,0,255},{0,127,0,255},{255,255,255,255}}; + Babl *palA = babl_new_palette (NULL); + Babl *palB = babl_new_palette (NULL); + + CHECK_CONV("pal to rgba", unsigned char, + palA, babl_format("RGBA u8"), + in, out); + + CHECK_CONV("pal to rgba", unsigned char, + palB, babl_format("RGBA u8"), + in, out); + + CHECK_CONV("pal to rgba", unsigned char, + palA, babl_format("RGBA u8"), + in, out); + } + + { + unsigned char in[][4] = {{0,0,0,255},{140,0,0,255},{0,127,0,255}}; + unsigned char out[][1] = {{ 0},{ 1},{ 2}}; + + CHECK_CONV("rgba to pal", unsigned char, + babl_format("RGBA u8"), babl_new_palette ("palC"), + in, out); + } + + /* check with a custom floating point palette */ + { + float palette[] = { + 0.5, 1.0, + 0.23, 0.42, + 1.0, 0.2 + }; + + unsigned char in[][1] = {{ 0},{ 1},{ 2}}; + unsigned char out[][4] = {{128,128,128,255},{59,59,59,107},{255,255,255,51}}; + + Babl *pal = babl_new_palette (NULL); + + babl_palette_set_palette (pal, babl_format ("YA float"), palette, 3); + + CHECK_CONV("rgba to YA float pal", unsigned char, + pal, babl_format("RGBA u8"), + in, out); + } + + babl_exit (); + return !OK; +} -- 2.30.2